package com.wc.alorithm_blue_bridge._思维.代码知音;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/12/20 9:44
 * @description
 * https://www.lanqiao.cn/problems/20087/learning/?contest_id=229
 */
public class Main {
    /**
     * 思路：分类讨论, 细节  a < b
     * a, b一个奇数一个偶数, 一定是不满足条件的, 奇数 * 奇数 = 奇数, 偶数 * 偶数 = 偶数
     * 所以 a, b 是同奇偶的
     * L >= 3
     * 为偶数的时候, a ^ b -> 2 ^ b * (a / 2) ^ b % P == 0, 同理 b ^ a % P == 0, 所以大于等于 3 的所有偶数数对都满足
     * 为奇数的时候, 可以发现 a ^ 2 % P == 1, 所以 a ^ b = a ^ (b - 1) * a % P = a % P, 同理 b ^ a % P = b % P, 那就是 a % P == b % P的时候, 满足条件
     * L == 2的时候, a = 2, 满足条件的只有 b % 4 == 0
     * L == 1的时候, a = 1, 满足条件的只有 b % 8 == 1
     * 全部条件都成立
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int P = (int)1e9 + 7;
    static long L, R;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            L = sc.nextLong();
            R = sc.nextLong();
            if (R <= 3) {
                out.println(0);
                continue;
            }
            long res = solve(Math.max(3, L), R);
            if (L <= 2) res = (res + R / 4) % P;
            if (L <= 1) res = (res + (R - 1) / 8) % P;
            out.println(res);
        }
        out.flush();
    }

    // L >= 3
    static long solve(long L, long R) {
        long res = 0;
        long n = R - L + 1;
        long even = n / 2, odd = even;
        if ((n & 1) == 1 && (L & 1) == 1) odd++;
        if ((n & 1) == 1 && (L & 1) == 0) even++;
        res = even % P * ((even - 1) % P) % P * qkm(2, P - 2) % P;
        // n % 4表示后面多了几个人, m = n / 4 表示有多少个 4 人组
        long t = odd % 4, m = odd / 4 % P;
        res = (res + m * (m - 1 + P) % P * (4 - t) % P * qkm(2, P - 2)) % P;
        res = (res + m * (m + 1) % P * t % P * qkm(2, P - 2) % P) % P;
        return res;
    }

    static long qkm(long a, long b) {
        long res = 1;
        while (b > 0) {
            if ((b & 1) == 1) res = res * a % P;
            a = a * a % P;
            b >>= 1;
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}